﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Jc.MicroDesigner.Management;
using Jc.MicroDesigner.Objects;

namespace Jc.MicroDesigner.ToolWindows
{
    /// <summary>
    /// Редактор генераторов импульсов
    /// </summary>
    public partial class ClockEditor : Form
    {
        private ObjectManager _Manager = null;
        private List<IClockGenerator> _Clocks = new List<IClockGenerator>();

        /// <summary>
        /// Создать форму редактора
        /// </summary>
        public ClockEditor()
        {
            InitializeComponent();
            StartTimeColumn.ValueType = typeof(Int32);
            TactColumn.ValueType = typeof(Int32);
            TicTime.ValueType = typeof(Int32);
            PulseTimeColumn.ValueType = typeof(Int32);
            SignalColumn.ValueType = typeof(SByte);
        }

        /// <summary>
        /// Показать диалог редактирования
        /// </summary>
        /// <param name="manager"></param>
        public void Show(ObjectManager manager)
        {
            _Manager = manager;
            for (int i = 0; i < manager.Generators.Count; i++)
                if (manager.Generators[i] is IClockGenerator)
                {
                    IClockGenerator clock = manager.Generators[i] as IClockGenerator;
                    _Clocks.Add(clock);
                    int row = ClocksTable.Rows.Add();
                    ClocksTable[0, row].Value = (clock as BaseElement).Name;
                    ClocksTable[1, row].Value = clock.Shift;
                    ClocksTable[2, row].Value = clock.TactTime;
                    ClocksTable[3, row].Value = clock.StartTime;
                    ClocksTable[4, row].Value = clock.ClockTime;
                    ClocksTable[5, row].Value = clock.Signal;
                }
            //Загрузить информацию
            ShowDialog();
        }

        private void CloseBtn_Click(object sender, EventArgs e)
        {
            //Сохранить значения
            for (int i = 0; i < _Clocks.Count; i++)
            {
                IClockGenerator cgen = _Clocks[i];
                cgen.Shift = Convert.ToInt32(ClocksTable[1, i].Value);
                cgen.TactTime = Convert.ToInt32(ClocksTable[2, i].Value);
                cgen.StartTime = Convert.ToInt32(ClocksTable[3, i].Value);
                cgen.ClockTime = Convert.ToInt32(ClocksTable[4, i].Value);
                cgen.Signal = Convert.ToSByte(ClocksTable[5, i].Value);
            }
        }

        private void ClocksTable_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > 0 && e.RowIndex >= 0)
            {
                object value = ClocksTable[e.ColumnIndex, e.RowIndex].Value;
                if ((Convert.ToInt32(value)) >= 0)
                    //Проверить соответствие типам данных и условиям
                    switch (e.ColumnIndex)
                    {
                        case 5:
                            sbyte signal = Convert.ToSByte(value);
                            if ((signal < 0) || (signal > 1))
                                ClocksTable[e.ColumnIndex, e.RowIndex].Value = 0;
                            break;
                    }
                else
                    ClocksTable[e.ColumnIndex, e.RowIndex].Value = 0;
            }
        }
    }
}